cmake_minimum_required(VERSION 2.8)
project(nano_self)

add_compile_options(-std=c++11)
add_compile_options(-pthread)


file(GLOB_RECURSE SOURCE_FILES src/*.cpp)
file(GLOB_RECURSE HEADER_FILES include/*.h)

include_directories(
    ${CMAKE_CURRENT_LIST_DIR}/../${PROJECT_NAME}/include    
)

add_executable(protoc IMPORTED)                                                     # 1
set_property(TARGET protoc PROPERTY IMPORTED_LOCATION "/usr/local/bin/protoc_")    # 2

set(GENERATED_SRC ${CMAKE_CURRENT_BINARY_DIR}/msg.pb.cc)
set(GENERATED_HEAD ${CMAKE_CURRENT_BINARY_DIR}/msg.pb.h)
add_custom_command(
	OUTPUT ${GENERATED_SRC} ${GENERATED_HEAD}
	COMMAND protoc --cpp_out=${CMAKE_CURRENT_BINARY_DIR} msg.proto
	WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/proto
	                                            # 3
)

file(GLOB_RECURSE HEADER_FILES include/*.h)
foreach(type server client)
    file(GLOB_RECURSE ${type}_FILES ${type}/*.cpp)
    add_executable(${PROJECT_NAME}_${type}
      ${${type}_FILES}
      ${HEADER_FILES}
      	${GENERATED_SRC}
	${GENERATED_HEAD}
    )


    target_link_libraries(${PROJECT_NAME}_${type}
        nanomsg
        protobuf
    )

    # add_custom_command(TARGET ${PROJECT_NAME}_${type} PRE_BUILD
    #                COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include)


    install(TARGETS ${PROJECT_NAME}_${type}
            DESTINATION lib
            )
endforeach()






# add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
#                    COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_BINARY_DIR})

# install(TARGETS ${PROJECT_NAME}
#         DESTINATION lib
#         )
install(DIRECTORY include/
        DESTINATION include)